package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    /**
     * 新增菜品
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @Cacheable(value = "setmealCache",key = "#setmealDTO.categoryId")
    public Result insert(@RequestBody SetmealDTO setmealDTO) {
        setmealService.insert(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result<PageResult> PageQuery(SetmealPageQueryDTO dto) {
        PageResult pageResult = setmealService.PageQuery(dto);
        return Result.success(pageResult);
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result deleteById(@RequestParam List<Long> ids) {
        setmealService.deleteById(ids);
        return Result.success();
    }

    /**
     * 根据id查询套餐,回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectWithDish(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.selectWithDish(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     *
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        //修改菜品表
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 修改状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result updateStatus (@PathVariable Integer status,Long id){
        setmealService.updateStatus(status, id);
        return Result.success();
    }
}